package com.fand.manager.controller;


import java.util.List;

import javax.annotation.Resource;

import com.fand.common.util.PageResult;
import com.fand.common.util.ResultHelp;
import com.fand.manager.model.user.SysUserDeleteDTO;
import com.fand.manager.model.user.SysUserPostDTO;
import com.fand.manager.model.user.SysUserReturnDTO;
import com.fand.manager.model.user.SysUserSearchDTO;
import com.fand.manager.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author javaboy
 * @since 2021-06-02
 */
@Api(tags = "用户信息")
@RestController
@RequestMapping("/sys-user")
public class SysUserController {

    @Resource
    private ISysUserService sysUserService;

    @ApiOperation("新增用户")
    @PreAuthorize("hasAuthority('sys:user:add')")
    @PostMapping("/save")
    public ResultHelp<Boolean> saveUser(@RequestBody SysUserPostDTO sysUserPostDTO) {
        return ResultHelp.success(sysUserService.saveUser(sysUserPostDTO));
    }

    @ApiOperation("查询用户")
    @PreAuthorize("hasAuthority('sys:user:query')")
    @GetMapping("/findUser")
    public ResultHelp<SysUserReturnDTO> findUser(@RequestParam Long id) {
        return ResultHelp.success(sysUserService.findUser(id));
    }

    @ApiOperation("分页查询所有")
    @PreAuthorize("hasAuthority('sys:user:query')")
    @GetMapping("/list")
    public PageResult<List<SysUserReturnDTO>> listUsers(SysUserSearchDTO sysUserSearchDTO) {
        return sysUserService.listUsers(sysUserSearchDTO.builder());
    }

    @ApiOperation("批量删除用户")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @PostMapping("/deleteByIds")
    public ResultHelp<Boolean> deleteByIds(@RequestBody SysUserDeleteDTO sysUserDeleteDTO) {
        return ResultHelp.success(sysUserService.deleteByIds(sysUserDeleteDTO.getUserIds()));
    }

    @ApiOperation("删除用户")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    @PostMapping("/delete")
    public ResultHelp<Boolean> deleteUser(Long id) {
        return ResultHelp.success(sysUserService.deleteUser(id));
    }

    @ApiOperation("修改用户")
    @PreAuthorize("hasAuthority('sys:user:update')")
    @PostMapping("/update")
    public ResultHelp<Boolean> updateUser(@RequestBody SysUserPostDTO sysUserPostDTO) {
        return ResultHelp.success(sysUserService.updateUser(sysUserPostDTO));
    }

}

